﻿using AutoMapper;
using MDT.LifeSewagePlatform.Conmmon;
using MDT.LifeSewagePlatform.Entity;
using MDT.LifeSewagePlatform.InterfaceService;
using MDT.LifeSewagePlatform.IRepository;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MDT.LifeSewagePlatform.Service
{
    public class RegionService:BaseService,IRegionService
    {
        [Autowired]
        protected IRegionRepository RegionRepository { get; set; }
        [Autowired]
        protected IMapper mapper { get; set; }
        public RegionService()
        {

        }
        public async Task<BaseMessage> AddAsync(RegionCreateOrUpdateDto RegionDto,int UserId)
        {
            var verifyResualt = RegionDto.Verify();
            if (verifyResualt.Item2 == false)
            {
                return BadRequest400(verifyResualt.Item1);

            }
            RegionEntity regionEntity=mapper.Map<RegionEntity>(RegionDto);
            await RegionRepository.Create(regionEntity,UserId);
            if (await RegionRepository.SaveChangesAsync())
            {
                return Ok200("添加成功!");
            }
            return BadRequest400("添加失败!");
            
        }

        public async Task<BaseMessage> UpdateAsync(int Id,RegionCreateOrUpdateDto RegionDto, int UserId)
        {
            #region params verify 
            var verifyResualt = RegionDto.Verify();
            if (verifyResualt.Item2 == false)
            {
                return BadRequest400(verifyResualt.Item1);
            }
            #endregion

            RegionEntity regionEntity =await RegionRepository.FirstOrDefaultAsync(t=>t.ID==Id);
            mapper.Map(RegionDto,regionEntity);
            await RegionRepository.Update(regionEntity, Id);
            if (await RegionRepository.SaveChangesAsync())
            {
                return Ok200("更新成功!");
            }
            return BadRequest400("更新失败!");
        }

        public async Task<BaseMessage> DeleteAsync(int Id)
        {
            await RegionRepository.Delete(t=>t.ID==Id);


            #region 限制删除
            
            #endregion
            if (await RegionRepository.SaveChangesAsync())
            {
                return Ok200("删除成功!");
            }
            return BadRequest400("删除失败!");
        }

        public async Task<BaseMessage> SoftDeleteAsync(int Id,int UserId)
        {
            
            await RegionRepository.SoftDelete(t => t.ID == Id,UserId);
            if (await RegionRepository.SaveChangesAsync())
            {
                return Ok200("删除成功!");
            }
            return BadRequest400("删除失败!");

        }

        public async Task<BaseMessage> RegionsAsync(RegionParameter Params)
        {
            var query=RegionRepository.GetRegionViews(t=>true);

            if (!string.IsNullOrEmpty(Params.Name))
            {
                query = query.Where(t=>t.DisplayName.Contains(Params.Name));
            }

            if (Params.ParentId>-1)
            {
                query = query.Where(t => t.ParentID==Params.ParentId);
            }

            if (Params.Id>-1)
            {
                query = query.Where(t => t.Id == Params.Id);
            }
            ///如果没有传参数类型默认查找省份
            if (Params.IsNull()||Params.Type>1)
            {
                query = query.Where(t => t.RegionType == Params.Type);
            }
            var data = query.ToArray();
            return Ok200("查询成功!",await PageList<RegionView>.CreateAsync(query, Params));
        }

        public async Task<BaseMessage> RegionsKeyAsync(RegionParameter Params)
        {
            var query = RegionRepository.GetRegionViews(t =>true);
            var data = query.ToArray();

            if (!string.IsNullOrEmpty(Params.Name))
            {
                query = query.Where(t => t.DisplayName.Contains(Params.Name));
            }

            if (Params.Id > -1)
            {
                query = query.Where(t => t.Id == Params.Id);
            }
            if (Params.ParentId > -1)
            {
                query = query.Where(t => t.ParentID== Params.ParentId);
            }

            if (Params.IsNull() || Params.Type > 1)
            {
                query = query.Where(t => t.RegionType == Params.Type);
            }

            var regiondata =query.
              Select(t => new RegionKeyView()
              {
                  Id = t.Id,
                  ParentId = t.ParentID,
                  Name = t.DisplayName,
                  type = t.RegionType
              });

            return Ok200("查询成功!",await regiondata.ToListAsync());
        }
    }
}
